package com.bdqn.ThymeleafJPA.controller;


import com.bdqn.ThymeleafJPA.config.Constants;
import com.bdqn.ThymeleafJPA.entity.User;
import com.bdqn.ThymeleafJPA.service.IUserService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

@Controller
public class LoginController {
    @Resource
    private IUserService userService;

    @GetMapping("/login")
    public String toLogin(){
        System.out.println("进入登录页面");
        return "login";
    }

    @PostMapping("/login")
    public String doLogin(Model model, HttpSession session, String usrName, String usrPassword){
        System.out.println("用户名："+usrName+"密码："+usrPassword);
        User user=userService.login(usrName,usrPassword);
        if(user==null){
            model.addAttribute("msg","用户名或密码错误");
            System.out.println("用户名或密码错误");
            return "login";
        }
        session.setAttribute(Constants.USER_SESSION,user);
        return "redirect:/main";
    }

    @GetMapping("/err")
    public String noAuth(){
        return "403";
    }
}
